package com.ctvit.sjk.knowledge.problem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctvit.sjk.knowledge.problem.mapper.TabProblemMapper;
import com.ctvit.sjk.knowledge.problem.pojo.domain.TabProblem;
import com.ctvit.sjk.knowledge.problem.pojo.domain.TabProblemParagraphMapping;
import com.ctvit.sjk.knowledge.problem.pojo.dto.ProblemGetVO;
import com.ctvit.sjk.knowledge.problem.pojo.dto.ProblemQueryDTO;
import com.ctvit.sjk.knowledge.problem.service.ITabProblemParagraphMappingService;
import com.ctvit.sjk.knowledge.problem.service.ITabProblemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 问题Service业务层处理
 *
 * @author heyingcheng
 * @date 2025/3/22
 */
@Service
public class TabProblemServiceImpl extends ServiceImpl<TabProblemMapper, TabProblem> implements ITabProblemService {

    private final ITabProblemParagraphMappingService tabProblemParagraphMappingService;

    public TabProblemServiceImpl(ITabProblemParagraphMappingService tabProblemParagraphMappingService) {
        this.tabProblemParagraphMappingService = tabProblemParagraphMappingService;
    }

    @Override
    public List<ProblemGetVO> list(String datasetId, ProblemQueryDTO dto) {
        LambdaQueryWrapper<TabProblem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TabProblem::getDatasetId, datasetId);
        wrapper.like(StringUtils.isNotBlank(dto.getContent()), TabProblem::getProblemContent, dto.getContent());
        wrapper.orderByDesc(TabProblem::getUpdateTime);

        return list(wrapper).stream().map(ProblemGetVO::new).collect(Collectors.toList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<ProblemGetVO> batchCreate(String datasetId, List<String> contentList, String createId) {
        List<String> idList = new ArrayList<>(10);

        for (String content : contentList) {
            String problemId = IdWorker.getIdStr();

            TabProblem problem = new TabProblem();
            problem.setProblemId(problemId);
            problem.setDatasetId(datasetId);
            problem.setProblemContent(content);
            problem.setCreateId(createId);
            problem.setCreateTime(LocalDateTime.now());
            problem.setUpdateId(createId);
            problem.setUpdateTime(LocalDateTime.now());
            save(problem);

            idList.add(problemId);
        }

        return listByIds(idList).stream().map(ProblemGetVO::new).collect(Collectors.toList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean batchRemove(String datasetId, List<String> problemIdList, String userId) {
        LambdaUpdateWrapper<TabProblemParagraphMapping> mappingWrapper = new LambdaUpdateWrapper<>();
        mappingWrapper.in(TabProblemParagraphMapping::getProblemId, problemIdList);
        mappingWrapper.eq(TabProblemParagraphMapping::getDatasetId, datasetId);
        tabProblemParagraphMappingService.remove(mappingWrapper);

        LambdaUpdateWrapper<TabProblem> problemWrapper = new LambdaUpdateWrapper<>();
        problemWrapper.set(TabProblem::getUpdateId, userId);
        problemWrapper.set(TabProblem::getUpdateTime, LocalDateTime.now());
        problemWrapper.in(TabProblem::getProblemId, problemIdList);

        return remove(problemWrapper);
    }

}
